package com.atguigu.ucenter.controller;


import com.atguigu.common.R;
import com.atguigu.common.cron.bean.PageListBean;
import com.atguigu.common.jwt.JwtUtils;
import com.atguigu.common.ordervo.UcenterMemberOrder;
import com.atguigu.servicebase.exceptionhandler.ELeanExeception;
import com.atguigu.ucenter.entity.Member;
import com.atguigu.ucenter.entity.vo.LoginVo;
import com.atguigu.ucenter.entity.vo.RegisterVo;
import com.atguigu.ucenter.enums.SessionConstEnum;
import com.atguigu.ucenter.service.MemberService;
import com.atguigu.ucenter.utils.SpringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author Panghl
 * @since 2021-02-03
 */
@RestController
@RequestMapping("/educenter/member/")
@Api(tags = {"用户管理"})
public class MemberController {

    @Autowired
    private MemberService memberService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @ApiOperation(value = "登录")
    @PostMapping(value = "login")
    public R login(@RequestBody LoginVo loginVo) {
//        String sessionCode = (String)SpringUtil.getRequest().getSession().getAttribute(SessionConstEnum.CODE_SESSION.toString());
        String verifCode = redisTemplate.opsForValue().get(SessionConstEnum.CODE_SESSION.toString());
        if (!StringUtils.isEmpty(loginVo.getCode()) && loginVo.getCode().equalsIgnoreCase(verifCode)) {
            //调用service方法实现登录
            //返回token值，使用jwt生成
            String token = memberService.login(loginVo);
            return R.ok().data("token", token);
        }
        return R.error();
    }

    @ApiOperation(value = "注册")
    @PostMapping(value = "register")
    public R register(@RequestBody RegisterVo registerVo) {
        memberService.register(registerVo);
        return R.ok();
    }

    @ApiOperation(value = "根据token获取用户信息")
    @GetMapping(value = "getMemberInfo")
    public R getMemberInfo(HttpServletRequest request) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        Member member = memberService.getMemberInfo(memberId);
        return R.ok().data("member", member);
    }

    @ApiOperation(value = "根据用户id获取用户信息")
    @PostMapping(value = "getUserInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable String id) {
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        Member member = memberService.getById(id);
        BeanUtils.copyProperties(member, ucenterMemberOrder);
        return ucenterMemberOrder;
    }

    @ApiOperation(value = "查询某一天注册人数")
    @GetMapping("countRegister/{day}")
    public R countRegister(@PathVariable String day) {
        Integer count = memberService.countRegister(day);
        return R.ok().data("countRegister", count);
    }

    @ApiOperation(value = "修改个人信息-签名-年龄")
    @PostMapping("editInfo")
    public R editInfo(@RequestBody(required = false) Member member) {
        try {
            if (!StringUtils.isEmpty(member)) {
                memberService.editInfo(member);
                return R.ok();
            }
            return R.error().data("更新用户信息不能为空");
        } catch (ELeanExeception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "根据用户id获取用户信息")
    @GetMapping(value = "getUserInfo/{id}")
    public R getUserInfo(@PathVariable String id) {
        Member member = memberService.getById(id);
        return  R.ok().data("member",member);
    }

    @ApiOperation(value = "获取我的课程信息")
    @GetMapping(value = "queryMyCourses/{pageNo}/{pageSize}")
    public R queryMyCourses(@PathVariable("pageNo") Integer pageNo,
                            @PathVariable("pageSize") Integer pageSize,
                            @RequestParam("memberId")String memberId) {
        PageListBean pageListBean = memberService.queryCoursesByMemberId(pageNo,pageSize,memberId);
        return  R.ok().data(pageListBean);
    }
}
